<?php

namespace App\Console\Commands;

use App\Jobs\CurveOrderPlan;
use App\Jobs\VisitRecord;
use App\Models\DomainList;
use App\Models\FlowChannel;
use App\Models\Order;
use App\Models\OrderReadInfo;
use App\Notifications\CommonMessage;
use App\Service\WxArticleData;
use Carbon\Carbon;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Artisan;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Notification;
use Illuminate\Support\Facades\Redis;

class Test extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'test';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Command description';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return int
     */
    public function handle()
    {

//        $order = Order::query()->findOrFail(773);
//
//        CurveOrderPlan::dispatch($order);dd(333);

        $redis = Redis::connection();

        $curve_order = $redis->srem('curve_order_list','curve_order_772');

        dd($curve_order);


        dd($curve_order);

//        dd(Order::query()->where(['state' => Order::ORDER_STATE_ON, 'type' => 0])->count());


        $order_read_info = OrderReadInfo::query()->whereDoesntHave('order')->where([
            'date'=>'2022-03-19',
            'hour' => 12
        ])->get();

        dd($order_read_info);

        $order = Order::query()->findOrFail(770);

        CurveOrderPlan::dispatch($order);dd(123);

        $total_read_num = 10000;

        $num = [];
        $min = 1;
        $max = 48;

        for ($i = $min; $i <= $max; $i++) {
            $num[] = sqrt(pow($i, 6));
        }
//        dd($num);

        $num = array_reverse($num);

        $total_index = array_sum($num);
        $read_num = [];

        $now_read_num = 0;
        foreach ($num as $item) {
//            print_r(($item / $total_index)."\r\n");
            $read_num_incr = ($item / $total_index) * $total_read_num;
//            print_r($read_num_incr."\r\n");
            $now_read_num += $read_num_incr;
            print_r($now_read_num . "\r\n");
//            $read_num[] = ($item / $total_index) * $total_read_num;
        }

        dd(123);

        $carbon = Carbon::now();
        $carbon->subDay();
        $last_hour = Carbon::now()->subHour();
        dd($last_hour->toDateString());


        dd($last_hour->toDateString() . ' ' . $last_hour->hour);

        $uv_num = \App\Models\VisitRecord::query()->where([
            'date' => '2022-03-14',
            'hour' => 10,
            'flow_channel_id' => 10
        ])
            ->distinct()->count('u_id');


        dd($uv_num);

//        $order = Order::query()->where('state', Order::ORDER_STATE_ON)->first();
//        $read_info = WxArticleData::get($order->url);
//
//
//
//        $random_order_list = [];
//        $bidding_order_list = [];
//        Order::query()->where('state', Order::ORDER_STATE_ON)->inRandomOrder()->get()->each(function ($order) use (&$random_order_list) {
//
//            $html = file_get_contents($order->url);
//
//            preg_match("/property=\"og:url\" content=\"(\S+)\#/", $html, $arr);
//
//            $url = str_replace('amp;', '', $arr[1]);
//            $random_order_list[] = [
//                'order_id' => $order->id,
//                'url' => $url
//            ];
//                $redis->rpush('random_order_list', serialize([
//                    'order_id' => $order->id,
//                    'url' => $url
//                ]));
//        });
//        Order::query()->where('state', Order::ORDER_STATE_ON)->orderByDesc('cpm')->get()->each(function ($order) use (&$bidding_order_list) {
//            $html = file_get_contents($order->url);
//
//            preg_match("/property=\"og:url\" content=\"(\S+)\#/", $html, $arr);
//
//            $url = str_replace('amp;', '', $arr[1]);
//            $bidding_order_list[] = [
//                'order_id' => $order->id,
//                'url' => $url
//            ];

//                $redis->rpush('bidding_order_list', serialize([
//                    'order_id' => $order->id,
//                    'url' => $url,
//                ]));
//        });

//        dd($bidding_order_list,$random_order_list);

//        $html =  <meta property="og:url" content="http://mp.weixin.qq.com/s?__biz=Mzk0ODI1NTc2NQ==&amp;mid=2247491463&amp;idx=1&amp;sn=33d2b7f71db99a52366016acba3e3fe7&amp;chksm=c36b32fdf41cbbebb30103e0a61238248bd70a673ca9afd4f93173b7ef62e64adda46a513ca4#rd" />

//        $html = file_get_contents('https://mp.weixin.qq.com/s/muFaD57mpSJL2XtRGqdQdA');
//
//        preg_match("/property=\"og:url\" content=\"(\S+)\#/", $html, $arr);
//
//        dd(str_replace('amp;','',$arr[1]));


//        $redis = Redis::connection();
//        dd($redis->keys('visit_record:2022-03-09*'));


//        dd(0301);

//        $domain = DomainList::query()->first();
//        $count = DomainList::query()->where('state', 0)->count();
//        Notification::send(new \stdClass(),
//            new CommonMessage('域名【' . $domain->domain . '】被封，当前系统剩余订单：' . $count . '个')
//        );
//        dd(123);


        $path = storage_path() . '/logs/';
        $redis = (new \Redis());
        $redis->connect('r-uf65pzu0zysyfpypnkpd.redis.rds.aliyuncs.com', 6379); //连接Redis
        $redis->auth('a7D3CCE421'); //密码验证
        $redis->select(1);//选择数据库


        for ($i = 0; $i <= 23; $i++) {
            for ($k = 0; $k < 60; $k++) {
                $data = $redis->hGetAll('ip_record:2022-03-09-' . $i . '-' . $k);
                foreach ($data as $key => $item) {
                    file_put_contents($path . '2022-03-09.txt', $key . '-' . $item . "\r\n", FILE_APPEND);
                }
            }

        }

        dd($path);
//        dd('ip_record:' . date('Y-m-d-H-i', time()), date('Y-m-d-H:m:s', time()) );
        in_array();


        dd($redis->hGetAll('ip_record:' . '2022-02-24' . '-19-35'));

        $redis = Redis::connection();
        $redis->zadd('ip_record:' . date('Y-m-d', time()), '123123');
        dd($redis->zrange('ip_record:' . date('Y-m-d', time()), 1, -1));
        for ($i = 0; $i < 225; $i++) {
            $visit_info = $redis->lpop('jump_record:2022-02-17');
            //VisitRecord::dispatch($visit_info);
        }
    }
}
